package com.example.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.R;
import com.example.domain.Rating;
import com.example.domain.model.MyPage;
import com.example.domain.model.RatingDTO;
import com.example.service.RatingService;
import com.example.shiro.AccountProfile;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@Api(tags = "评分管理")
@RestController
@RequestMapping("/rating")
@RequiresAuthentication
public class RatingController {

    @Autowired
    private RatingService ratingService;

    @ApiOperation(value = "评分列表")
    @PostMapping("/list")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页索引", defaultValue = "1"),
            @ApiImplicitParam(name = "size", value = "每页显示记录数", defaultValue = "20")
    })
    public R<IPage<RatingDTO>> getList(@RequestBody MyPage myPage){
        //获取当前用户信息
        AccountProfile account = (AccountProfile) SecurityUtils.getSubject().getPrincipal();

        Page<Rating> page = new Page(myPage.getCurrent(),myPage.getSize());
        return R.success(ratingService.getList(account.getId(),page));
    }

    @ApiOperation(value = "我的评分")
    @GetMapping("/{movieId}")
    public R<Rating> getInfo(@PathVariable String movieId){
        //获取当前用户信息
        AccountProfile account = (AccountProfile) SecurityUtils.getSubject().getPrincipal();

        return R.success(ratingService.getByMovieId(account.getId(),movieId));
    }

    @ApiOperation(value = "新增评分")
    @PostMapping()
    public R add(@RequestBody Rating rating){
        //获取当前用户信息
        AccountProfile account = (AccountProfile) SecurityUtils.getSubject().getPrincipal();

        return ratingService.save(rating,account.getId()) ? R.success() : R.error();
    }
    @ApiOperation(value = "更新评分")
    @PutMapping()
    public R update(@RequestBody Rating rating){
        //获取当前用户信息
        AccountProfile account = (AccountProfile) SecurityUtils.getSubject().getPrincipal();
        return ratingService.update(account.getId(),rating) ? R.success() : R.error();
    }

    @ApiOperation(value = "删除评分")
    @DeleteMapping("/{id}")
    public R delete(@PathVariable String id){
        return ratingService.delete(id) ? R.success() : R.error();
    }
}
